package com.chaoxuan.uebt.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.activity.NewDetailActivity;
import com.chaoxuan.uebt.adapter.InfoAdapter;
import com.chaoxuan.uebt.model.News;
import com.chaoxuan.uebt.model.Result;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * Created by Administrator on 2017/3/13.
 */
public class InfoFragment extends Fragment implements OnRefreshListener,OnLoadMoreListener,BaseQuickAdapter.OnItemClickListener {

    private View rootView;
    SmartRefreshLayout refreshLayout;
    RecyclerView recyclerView;
    InfoAdapter adapter;
    int page;
    String type="1";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_info, container, false);


        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        refreshLayout = (SmartRefreshLayout)rootView.findViewById(R.id.refreshLayout);

        adapter= new InfoAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        init();
        return rootView;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        News news=(News) adapter.getData().get(position);
        if(news==null)return;
        Intent i=new Intent(getActivity(),NewDetailActivity.class);
        i.putExtra("id",news.getId());
        startActivity(i);
    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {

        } else {
            init();
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page=0;
        load(true,refreshLayout);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page=adapter.getData().size();
        load(false, refreshLayout);

    }

    private void init() {
        refreshLayout.autoRefresh();
    }

    private void load(final boolean refresh,final RefreshLayout refreshLayout){
        RetrofitUtils.getAPIService().indexNews(page,type).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Result<List<News>>>(){
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if(refresh)
                    refreshLayout.finishRefresh(false);
                else{
                    refreshLayout.finishLoadMore(false);
                }
                e.printStackTrace();
            }

            @Override
            public void onNext(Result<List<News>> data) {
                if(refresh){
                    adapter.replaceData(data.getData());
                    refreshLayout.finishRefresh();
                }else{
                    adapter.addData(data.getData());
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }


}
